﻿'Copyright (c) 2008, Jonathan Allen

Imports ClrExtensions.Net.Rest
Imports ClrExtensions.CollectionExtension


''' <summary>
''' This object is used to access general data in the Netflix catalog.
''' </summary>
''' <remarks>Catalog is threadsafe</remarks>
Public Class Catalog
	Private m_Connection As NetflixConnection

	Friend Sub New(ByVal connection As NetflixConnection)
		m_Connection = connection
	End Sub

	''' <summary>
	''' This returns the entire Netflix catalog as a StreamReader. 
	''' </summary>
	''' <returns></returns>
	''' <remarks>At this time of this writing it is over 179 MB.</remarks>
	Public Function IndexStream() As IO.StreamReader
		Return Connection.RequestResourceStream(NetflixUrls.GetCatalogTitlesIndex)
	End Function

	Public Sub DownloadIndex(ByVal filename As String)
		Using incomingStream = IndexStream()
			Using file = New IO.StreamWriter(IO.File.OpenWrite(filename))
				Dim s = incomingStream.ReadLine
				Do Until s Is Nothing
					file.WriteLine(s)
					s = incomingStream.ReadLine
				Loop
			End Using
		End Using
	End Sub

	'''' <summary>
	'''' This function peforms a simple title search
	'''' </summary>
	'''' <param name="searchTerm"></param>
	'''' <returns></returns>
	'''' <remarks></remarks>
	'Public Function TitleSearch(ByVal searchTerm As String) As CatalogTitles
	'	Return TitleSearchByPage(searchTerm, Nothing, Nothing)
	'End Function

	'''' <summary>
	'''' This function performs a pagable title search
	'''' </summary>
	'''' <param name="searchTerm"></param>
	'''' <param name="startIndex">Zero based index for the first title to return</param>
	'''' <param name="maxResultsPerPage">Maximum number of results to return per page</param>
	'''' <returns></returns>
	'''' <remarks></remarks>
	'Public Function TitleSearchByPage(ByVal searchTerm As String, ByVal startIndex As Integer?, ByVal maxResultsPerPage As Integer?) As CatalogTitles
	'	Dim raw = Connection.RequestResource(NetflixUrls.GetCatalogTitles(searchTerm, startIndex, maxResultsPerPage))
	'	Return New CatalogTitles(Connection, raw)
	'End Function

	''' <summary>
	''' This function preforms a series of search requests, concatinating the result
	''' </summary>
	''' <param name="searchTerm"></param>
	''' <param name="maxResults"></param>
	''' <returns></returns>
	''' <remarks></remarks>
	Public Function TitleSearch(ByVal searchTerm As String, ByVal maxResults As Integer) As CatalogTitleCollection
		Dim result As New List(Of CatalogTitle)
		Dim runningStartIndex = 0

		Do
			Dim listFragment = Connection.RequestXmlResource(NetflixUrls.GetCatalogTitles(searchTerm, runningStartIndex, Math.Min(100, maxResults - result.Count)))
			If listFragment.<catalog_title>.Count = 0 Then Exit Do
			For Each node In listFragment.<catalog_title>
				result.Add(CatalogTitle.ParseEntry(Connection, node))
				If result.Count >= maxResults Then Exit For
			Next
			If result.Count >= maxResults Then Exit Do
			runningStartIndex += listFragment.<catalog_title>.Count
		Loop

		Return New CatalogTitleCollection(result)
    End Function

    Public Function TitleSearch(ByVal searchTerm As String, ByVal maxResults As Integer, ByVal expansions As TitleExpansions) As CatalogTitleCollection
        Dim result As New List(Of CatalogTitle)
        Dim runningStartIndex = 0

        Do
            Dim listFragment = Connection.RequestXmlResource(NetflixUrls.GetCatalogTitles(searchTerm, runningStartIndex, Math.Min(100, maxResults - result.Count), expansions))
            If listFragment.<catalog_title>.Count = 0 Then Exit Do
            For Each node In listFragment.<catalog_title>
                result.Add(CatalogTitle.ParseEntry(Connection, node))
                If result.Count >= maxResults Then Exit For
            Next
            If result.Count >= maxResults Then Exit Do
            runningStartIndex += listFragment.<catalog_title>.Count
        Loop

        Return New CatalogTitleCollection(result)
    End Function

    Public Function GetTitle(ByVal id As String) As CatalogTitle
        Dim title = Connection.RequestXmlResource(New RestCall(RestVerb.Get, id))
        Return CatalogTitle.ParseEntry(Connection, title)
    End Function

    Public Function TitlesForAutocomplete(ByVal searchTerm As String) As List(Of String)
        Dim raw = Connection.RequestResource(NetflixUrls.GetCatalogTitlesAutocomplete(searchTerm))
        Dim source = XDocument.Parse(raw)
        Return (From node In source...<title> Select node.@short).ToList
    End Function

    Friend ReadOnly Property Connection() As NetflixConnection
        Get
            Return m_Connection
        End Get
    End Property

    'Public Function GetTitleDetails(ByVal id As String, ByVal expansions As TitleExpansions) As CatalogTitle
    '	Dim temp = New RestCall(RestVerb.Get, id)
    '	temp = temp.AddParameter(expansions.ToQueryParameter)
    '	Dim raw = Connection.RequestResource(temp)
    '	Return New CatalogTitle(Connection, raw)
    'End Function



End Class
